import Vue from 'vue'
import Vuex from 'vuex'
import {MT_ADD,MT_SUB} from './mutation-types.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    count: 0,
    message: 'hell',
    books:[
      {
        isChecked: true,
        id: 1,
        tdate: '2020-10',
        price: 100,
        count: 2,
        name: '《Java从入门到放弃》',
      },
      {
        isChecked: true,
        id: 2,
        tdate: '2010-10',
        price: 90.01,
        count: 1,
        name: '《PHP从入门到放弃》',
      },
      {
        isChecked: false,
        id: 3,
        tdate: '2012-10',
        price: 80,
        count: 1,
        name: '《Python从入门到放弃》',
      },
      {
        isChecked: false,
        id: 4,
        tdate: '2013-10',
        price: 70,
        count: 1,
        name: '《什么都是从入门到放弃》',
      }

    ]
  },
  getters: {
     ckBooks(state){
       return state.books.filter(item => item.isChecked)
     },
     ckBooksLength(state,getters){
       return getters.ckBooks.length;
     },
     searchById(state){
       return function(id){
         return state.books.find(item => item.id === id)
       }
     }
  },
  mutations: {
    [MT_SUB](state){
      state.count--
    },
    [MT_ADD](state){
      state.count++
    },
    mtAddN(state,payload){
      //payload 普通值，对象，函数
      state.count += payload
    },
    mtHs(state, payload) {
      //payload 普通值，对象，函数
      console.log(payload(state.message))
    },
    mtDx(state, payload) {
      //payload 普通值，对象，函数
      console.log(payload)
    },
    mtYb(state){
      setTimeout(() => state.count++,1000)
    }

  },
  actions: {
    acAdd(context){
      setTimeout(() => {
        console.log("请求后台请求完数据");
        context.commit('mtAdd')
      },1000)
      
    }
  },
  modules: {
  }
})
